﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DGSFramework;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace Framework
{
    public partial class Default : System.Web.UI.Page
    {
        public DGSFrameworkEntities entity = new DGSFrameworkEntities();

        public List<udv_Categories> allCate = new List<udv_Categories>();
        public List<udv_Articles> allArt = new List<udv_Articles>();
        public udv_Articles udvArticle = new udv_Articles();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadCacheArticle();
                GetTopArticle();
                LoadSlide();
            }
        }
        public void LoadCacheArticle()
        {
            if (Cache["Article"] == null)
            {
                Cache["Article"] = allArt = entity.udv_Articles.Include("udv_Categories").ToList();
            }
            else
            {
                allArt = (List<udv_Articles>)Cache["Article"];
            }
        }
    
        public void GetTopArticle()
        {
            try
            {
                var topEvent = entity.udv_Articles.Where(a => a.StatusID == 1).OrderBy(a => a.DisplayOrder).ThenByDescending(a => a.AddedDate).Take(2);
                rptArt.DataSource = topEvent;
                rptArt.DataBind();

            }
            catch (Exception)
            {

                throw;
            }
        }

        protected void LoadSlide()
        {
            try
            {
                XDocument xDocImage = XDocument.Load(Server.MapPath(ConfigPaths.Path_Banner_Xml));
                var xEImage = xDocImage.Descendants("Slide")
                    .Select(sl => new
                    {
                        src = sl.Attribute("src").Value,
                        title = sl.Attribute("title").Value.Trim(),
                        title_en = sl.Attribute("title_en").Value.Trim(),
                        description = sl.Element("description").Value.Trim(),
                        description_en = sl.Element("description_en").Value.Trim(),
                        cateID = sl.Attribute("cateID").Value.Trim(),
                        link = !string.IsNullOrEmpty(sl.Attribute("link").Value) ? sl.Attribute("link").Value : "#",
                        index = int.Parse(sl.Attribute("index").Value),
                        alt = sl.Attribute("alt").Value
                    }).Where(sl => sl.cateID == "0").OrderBy(sl => sl.index).ToList();
                rptSlide.DataSource = xEImage;
                rptSlide.DataBind();
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}